{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Test notebook lazy pipeline"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Installed packages\n",
    "import pandas as pd\n",
    "\n",
    "# Testing\n",
    "from IPython.utils.capture import capture_output\n",
    "\n",
    "# Our package\n",
    "from ydata_profiling import ProfileReport\n",
    "from ydata_profiling.utils.cache import cache_file"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Read the Titanic Dataset\n",
    "file_name = cache_file(\n",
    "    \"titanic.csv\",\n",
    "    \"https://raw.githubusercontent.com/datasciencedojo/datasets/master/titanic.csv\",\n",
    ")\n",
    "df = pd.read_csv(file_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate the Profiling Report (with progress bar)\n",
    "with capture_output() as out:\n",
    "    profile = ProfileReport(df, title=\"Titanic Dataset\", progress_bar=True, lazy=False)\n",
    "\n",
    "assert all(\n",
    "    any(v in s.data[\"text/plain\"] for v in [\"%|\", \"FloatProgress\"]) for s in out.outputs\n",
    ")\n",
    "assert len(out.outputs) == 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Generate the Profiling Report (without progress bar)\n",
    "with capture_output() as out:\n",
    "    profile = df.profile_report(\n",
    "        title=\"Titanic Dataset\",\n",
    "        html={\"style\": {\"full_width\": True}},\n",
    "        progress_bar=True,\n",
    "        lazy=True,\n",
    "    )\n",
    "\n",
    "assert len(out.outputs) == 0\n",
    "\n",
    "with capture_output() as out:\n",
    "    _ = profile.to_html()\n",
    "\n",
    "\n",
    "assert all(\n",
    "    any(v in s.data[\"text/plain\"] for v in [\"%|\", \"FloatProgress\"]) for s in out.outputs\n",
    ")\n",
    "assert len(out.outputs) == 3\n",
    "\n",
    "with capture_output() as out:\n",
    "    _ = profile.to_file(\"/tmp/tmpfile.html\")\n",
    "\n",
    "assert \"Export report to file\" in out.outputs[0].data[\"text/plain\"]\n",
    "assert len(out.outputs) == 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Test caching of the iterative building process\n",
    "with capture_output() as out:\n",
    "    profile = ProfileReport(df, title=\"Titanic Dataset\", progress_bar=True, lazy=True)\n",
    "assert len(out.outputs) == 0\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.description_set\n",
    "assert len(out.outputs) == 1\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.report\n",
    "assert len(out.outputs) == 1\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.html\n",
    "assert len(out.outputs) == 1\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.config.html.style.theme = \"united\"\n",
    "    profile.invalidate_cache(\"rendering\")\n",
    "    profile.to_file(\"/tmp/cache1.html\")\n",
    "assert len(out.outputs) == 2\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.config.pool_size = 1\n",
    "    profile.html\n",
    "assert len(out.outputs) == 0\n",
    "\n",
    "with capture_output() as out:\n",
    "    profile.config.pool_size = 0\n",
    "    profile.config.samples.head = 5\n",
    "    profile.config.samples.tail = 15\n",
    "    profile.invalidate_cache()\n",
    "    profile.to_file(\"/tmp/cache2.html\")\n",
    "assert len(out.outputs) == 4"
   ]
  }
 ],
 "metadata": {
  "language_info": {
   "name": "python"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
